package com.huanshi.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.huanshi.entity.UmsAddress;
import com.huanshi.mapper.UmsAddressMapper;
import com.huanshi.service.UmsAddressService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 幻世
 * @since 2023-10-10
 */
@Service
public class UmsAddressServiceImpl extends ServiceImpl<UmsAddressMapper, UmsAddress> implements UmsAddressService {

    @Resource
    UmsAddressMapper addressMapper;
    @Override
    public List<UmsAddress> getByUserId(Integer userId) {
        return addressMapper.selectList(new LambdaQueryWrapper<UmsAddress>().eq(UmsAddress::getUserId, userId).orderByDesc(UmsAddress::getIsFlag));
    }

    @Override
    public UmsAddress getDefaultAddress(Long userId) {
        return addressMapper.selectOne(new LambdaQueryWrapper<UmsAddress>().eq(UmsAddress::getUserId, userId).eq(UmsAddress::getDefaultFlag, 1));
    }

    @Override
    public Integer updateFlag(Long addressId, Long userId) {
        addressMapper.update(null, new LambdaUpdateWrapper<UmsAddress>().eq(UmsAddress::getUserId, userId).set(UmsAddress::getIsFlag, 0));

        return addressMapper.update(null, new LambdaUpdateWrapper<UmsAddress>().eq(UmsAddress::getId, addressId).set(UmsAddress::getIsFlag, 0));
    }
}
